import { isSameObect, deepClone } from '@/utils/common';
import { toRaw } from 'vue';
export default function useIsParamsChange(query: Record<string, any>) {
	let flag = true;
	let lastQuery: Record<string, any> = {};
	// 初始化查询参数存储起来
	lastQuery = getFormatQuery(query);

	//获取上次的对象
	function getFormatQuery(query: Record<string, any>) {
		let obj = null;
		try {
			if (query.data && typeof JSON.parse(query.data) === 'object') {
				// query参数是json字符串
				obj = JSON.parse(query.data);
			} else {
				obj = query;
			}
		} catch (err) {
			console.log(err);
		}
		return deepClone(obj);
	}
	function isParamsChange(query: Record<string, any>) {
		const newQuery = getFormatQuery(query);

		flag = !isSameObect(newQuery, toRaw(lastQuery));
		lastQuery = deepClone(newQuery);
		return flag;
	}
	return {
		isParamsChange,
	};
}
